package com.example.capturetheflag.joinGame;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.capturetheflag.R;
import com.example.capturetheflag.helper.GameFieldSimple;
import com.example.capturetheflag.helper.PlayerMarker;
import com.example.capturetheflag.helper.Singleton;
import com.example.capturetheflag.helper.SitesOverlay;
import com.example.capturetheflag.helper.ThreeSecondRequest;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
//DDMS Lon:18,584 Lat:54,422


public class GameActivity extends MapActivity implements LocationListener {
	
	private static Boolean PLAYER_CENTER = false;
	private static Boolean FIELD_CENTER = false;
	
	public static int GlobalIdGry = 0;
	
	private MapView mapView;
	private GameFieldSimple gameFieldSimple;
	private PlayerMarker playerMarker;
	private MapController mapController;
	private Button bt;
	private List<Overlay> listaWarstw;
	private Boolean wasCentered, wasFieldLock, serverConnection;
	private Dialog dialog;
	private Dialog dialogArea;
	
	private Handler handler;
	private ThreeSecondRequest tsr;
	private SitesOverlay sites;
	
	private int resource;
	
	@Override
  public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
      
      setContentView(R.layout.activity_game);

  	  bt = (Button)findViewById(R.id.setField2);
      
      mapView = (MapView) findViewById(R.id.mapview);
      
      mapController = mapView.getController();
      
      listaWarstw = mapView.getOverlays();
      
      LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
      
      sites=new SitesOverlay();
      //sites.add();
      
      if(Singleton.getInstance().team) 
      	resource = R.drawable.you_a;
      else
      	resource = R.drawable.you_b;
      
      Drawable drawable = this.getResources().getDrawable(resource);
      playerMarker = new PlayerMarker(drawable, this);
      //itemizedOverlay = new MyItemizedOverlay(drawable);
      
      listaWarstw.add(playerMarker);
      
      wasCentered = false;
      wasFieldLock = false;
      serverConnection = false;
      mapView.setSatellite(false);  
      mapView.setTraffic(false);
      
      dialog = new Dialog(this);
      dialog.setTitle("Please Wait for GPS..");
      dialog.show();
      dialog.setCancelable(false);

      dialogArea = new Dialog(this);
      dialogArea.setTitle("Go back to game area !!!");
      dialogArea.setCancelable(false);
      
      mapView.setClickable(false);
      
      setTitle("gameID:" + Singleton.getInstance().gameID +"  playerID:"+Singleton.getInstance().playerID);
      
      gameFieldSimple = new GameFieldSimple();
      listaWarstw.add(gameFieldSimple);
      wasFieldLock = true;
      mapView.setClickable(false);
  }
	
  @Override
	public boolean onCreateOptionsMenu(Menu menu) {
  	MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.layout.game_menu_2, menu);
		return super.onCreateOptionsMenu(menu);
	}
  
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      	case R.id.startServerConnection:
      		if(!serverConnection){
      			handler = new Handler();
      			tsr = new ThreeSecondRequest(handler,sites,mapView);
      			handler.postDelayed(tsr, 1000);
      			serverConnection = true;
      			listaWarstw.add(sites);
      			Toast.makeText(this, "Connection to the server", Toast.LENGTH_SHORT).show();
      			mapView.getController().zoomIn();
      		}else{
      			handler.removeCallbacks(tsr);
      			serverConnection = false;
      			Toast.makeText(this, "Disconnection to the server", Toast.LENGTH_SHORT).show();
      		}
      		return true;
	        case R.id.zoomIn:
				mapView.getController().zoomIn();
	            return true;
	        case R.id.zoomOut:
				mapView.getController().zoomOut();
	            return true;
	        case R.id.menu:
				finish();
              return true;
          case R.id.satelite:
				mapView.setSatellite(true);
              return true;
          case R.id.map:
				mapView.setSatellite(false);
              return true;
          default:
              return super.onOptionsItemSelected(item);
      }
  }

	@Override
	public void onBackPressed() 
	{
		Toast.makeText(this, "To back to main screen or exit, go to settings and pres 'MENU'", Toast.LENGTH_SHORT).show();
	}
	
	public void myClickHandler (View target) {
		switch(target.getId()) {
			case R.id.setField2:
					if(!mapView.isClickable())
					{
						if(!FIELD_CENTER)
						{
							mapController.animateTo(gameFieldSimple.getCenterOfField());
				        	bt.setText("Go To Player");
				        	FIELD_CENTER = true;
							PLAYER_CENTER = false;
						}
						else
						{
							PLAYER_CENTER = true;
				        	FIELD_CENTER = false;
			        		bt.setText("Go To Field");
						}
				}
				break;
	     } 
	}
  
  public void onLocationChanged(Location location) {
	  
	int points = Singleton.getInstance().points;
	if(points == -1){
		dialogArea.show();
	}else{
		dialogArea.dismiss();
	}
	
	if(points == -2){
		Toast.makeText(this, "THE END, The Reds wins !!!!", Toast.LENGTH_LONG).show();
	}else 
	if (points == -3){
		Toast.makeText(this, "THE END, The Blues wins !!!", Toast.LENGTH_LONG).show();
	}
	
	  
  	if(playerMarker.getmOverlays().size()>0)
  		playerMarker.getmOverlays().remove(0);
  	
  	if(dialog.isShowing())
  		dialog.dismiss();
  	
      Log.v("test", "location received ! "+location.getLatitude()+";"+location.getLongitude());
      
      int x = (int)(location.getLatitude()*1000000);
      int y = (int)(location.getLongitude()*1000000);
      GeoPoint me = new GeoPoint( x,y );
      Singleton.getInstance().latitude = x;
      Singleton.getInstance().longitude = y;
      
      if(Singleton.getInstance().flag){
   
      		if(Singleton.getInstance().team)
          		resource = R.drawable.you_a_with_flag;
          	else
          		resource = R.drawable.you_b_with_flag;
    	  
      }else{
    	  if(Singleton.getInstance().team) 
          		resource = R.drawable.you_a;
          	else
          		resource = R.drawable.you_b;
      }
      	
  
      
      Drawable drawable = this.getResources().getDrawable(resource);
      playerMarker = new PlayerMarker(drawable, this);
      listaWarstw.add(playerMarker);
      
      OverlayItem overlayitem = new OverlayItem(me, "To ty!", "8D");
      playerMarker.addOverlay(overlayitem);
      
      if(!wasCentered)
      {
      	mapController.setCenter(me);
      	wasCentered = true;
          mapController.setZoom(16);
          //mapView.setClickable(true);
      }
      if(!mapView.isClickable())
      {
      	if(PLAYER_CENTER && !FIELD_CENTER)
          	mapController.animateTo(me);
      	else if(!FIELD_CENTER && !PLAYER_CENTER){
      		bt.setText("use menu button");
      		mapView.setClickable(false);
      	}
      	
      }
      
	        TextView t=new TextView(this); 
	        t=(TextView)findViewById(R.id.points); 
	        int a = Singleton.getInstance().points;
	        t.setText("current-points: "+Singleton.getInstance().points);
      
  }

  public void onProviderDisabled(String provider) {
      Log.v("test", "location provider disabled");

  }

  public void onProviderEnabled(String provider) {
      Log.v("test", "location provider enabled");

  }

  public void onStatusChanged(String provider, int status, Bundle extras) {
      Log.v("test", "location provider status changed");

  }

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	@Override
   protected boolean isLocationDisplayed() {
       return false; 
   }
	
	@Override
  public void onDestroy()
  {
	
    super.onDestroy();

  }
}
